﻿using System.Collections.ObjectModel;
using System.Globalization;
using System.IO;
using System.Text.RegularExpressions;

namespace GlobalizationTools.Model
{
    public class Solution
    {
        private static Regex solutionRegex = new Regex(@"Project\(""\{(.*)\}""\) = ""(.*)"", ""(.*)"", ""\{(.*)\}""");

        string _filePath;
        Collection<Project> _projects = new Collection<Project>();
        bool _isLoaded = false;

        public Solution(string filePath)
        {
            _filePath = filePath;
        }

        public void LoadSolution()
        {
            FileStream stream = new FileStream(_filePath, FileMode.Open, FileAccess.Read, FileShare.Read);
            StreamReader reader = new StreamReader(stream);
            string content = reader.ReadToEnd();
            reader.Close();

            this.Projects.Clear();
            MatchCollection matches = solutionRegex.Matches(content);
            foreach (Match match in matches)
            {
                string filePath = Path.Combine(Path.GetDirectoryName(this.FilePath), match.Groups[3].Value);
                string extension = Path.GetExtension(filePath);
                if (extension.Length > 4 && extension.EndsWith("proj", true, CultureInfo.CurrentCulture))
                {
                    string projectName = match.Groups[2].Value;
                    Project project = new Project(filePath, projectName, this);
                    this._projects.Add(project);
                }
            }
            _isLoaded = true;
        }

        public string FilePath
        {
            get { return _filePath; }
        }

        public Collection<Project> Projects
        {
            get { return _projects; }
        }

        public bool IsLoaded
        {
            get { return _isLoaded; }
        }


    }
}
